package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.erp.ErpBarcodeTemplate;
import com.nuoniu.sibanyun.mapper.erp.ErpBarcodeTemplateMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpBarcodeTemplateService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 条码模版 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-11-13
 */
@Service
public class ErpBarcodeTemplateServiceImpl extends ServiceImpl<ErpBarcodeTemplateMapper, ErpBarcodeTemplate> implements IErpBarcodeTemplateService {

    @Autowired
    private IErpAccountService accountService;

    @Override
    public List<ErpBarcodeTemplate> getList(ErpBarcodeTemplate template) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", UserThreadLocal.get().getCompanyId());
        if(template.getCode() != null) {
            wrapper.likeRight("code", template.getCode());
        }
        if(StringUtils.isNotBlank(template.getApplicableObject())){
            wrapper.eq("applicable_object",template.getApplicableObject());
        }
        if(null != template.getIsDefault()){
            wrapper.eq("is_default",template.getIsDefault());
        }
        return list(wrapper);
    }

    @Override
    public void saveTemplate(ErpBarcodeTemplate barcodeTemplate) {
        UserInfo userInfo = UserThreadLocal.get();
        if(barcodeTemplate.getId() == null) {
            barcodeTemplate.setAccountId(accountService.getCurrentAccountId());
            barcodeTemplate.setCompanyId(userInfo.getCompanyId());
        }

        this.saveOrUpdate(barcodeTemplate);
    }
}
